<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" label-width="120px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <!--      <el-form-item label="备注" prop="note">
              <el-input v-model="dataForm.note" placeholder="备注"></el-input>
            </el-form-item>-->
      <!--      <el-form-item label="分类id" prop="cateId">-->
      <!--        <el-input v-model="dataForm.cateId" placeholder="分类id"></el-input>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="分类" prop="cateName">
              &lt;!&ndash;        <el-input v-model="dataForm.cateName" placeholder="分类名称"></el-input>&ndash;&gt;
              <ren-cate-tree v-model="dataForm.cateId" v-model:treeName="dataForm.cateName" placeholder="选择分类"
                             :query="true"></ren-cate-tree>
            </el-form-item>-->
      <el-form-item label="推荐" prop="recommend">
        <el-radio-group v-model="dataForm.recommend">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">下线</el-radio>
          <el-radio :label="1">上线</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="图片" prop="image" :rules="{ required: '必传' }">
        <el-upload :action="dataForm.upload" :show-file-list="false" :before-upload="beforeUploadHandle" :on-success="successHandle">
          <el-image v-if="dataForm.image" :src="dataForm.image" class="avatar" style="max-width: 200px"></el-image>
          <el-icon v-else :size="30">
            <Plus />
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="附项名称" prop="appendixOneName">
        <el-input v-model="dataForm.appendixOneName" placeholder="请输入附项名称"></el-input>
      </el-form-item>
      <el-form-item label="附项单价" prop="appendixOnePrice">
        <el-input v-model="dataForm.appendixOnePrice" placeholder="请输入附项单价"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <wang-eidtor v-model="dataForm.content" placeholder="请输入内容"></wang-eidtor>
      </el-form-item>
      <!--      <el-form-item label="检测标准" prop="standards">
              <el-input type="textarea" :rows="3" placeholder="请输入检测标准" v-model="dataForm.standards"></el-input>
            </el-form-item>
            <el-form-item label="适用范围" prop="ranges">
              <el-input type="textarea" :rows="3" placeholder="请输入适用范围" v-model="dataForm.range">
              </el-input>
            </el-form-item>-->

      <!--      <el-form-item label="价格" prop="price">
              <el-input v-model="dataForm.price" placeholder="价格"></el-input>
            </el-form-item>
            <el-form-item label="创建时间" prop="createTime">
              <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
            </el-form-item>
            <el-form-item label="更新时间" prop="updateTime">
              <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
            </el-form-item>-->
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
import { IObject } from "@/types/interface";
import { getToken } from "@/utils/cache";
import WangEidtor from "@/components/wang-editor/index.vue";

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  name: "",
  image: "",
  content: "",
  note: "",
  cateId: "",
  cateName: "",
  recommend: 0,
  status: 0,
  appendixOneName: "",
  appendixOnePrice: "",
  upload: ""
  /*
      standards: '',
      ranges: '',
      price: '',
      createTime: '',
      updateTime: ''*/
});
const rules = ref({
  name: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  image: [{ equired: true, message: "必填项不能为空", trigger: "blur" }],
  note: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  // cateName: [{required: true, message: '必填项不能为空', trigger: 'blur'}],
  content: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  /*standards: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  ranges: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  cateId: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],,*/
  recommend: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  status: [{ required: true, message: "必填项不能为空", trigger: "blur" }]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";
  dataForm.upload = `${window.SITE_CONFIG["apiURLUpload"]}?token=${getToken()}&type=project`;

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/yckj/project/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 上传之前
const beforeUploadHandle = (file: IObject) => {
  if (file.type !== "image/jpg" && file.type !== "image/jpeg" && file.type !== "image/png" && file.type !== "image/gif") {
    ElMessage.error("只支持jpg、png、gif格式文件！");
    return false;
  }
};

// 上传成功
const successHandle = (res: IObject, file: IObject, list: IObject[]) => {
  dataForm.image = res.data.url;
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/yckj/project", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
